﻿#define _CRT_SECURE_NO_WARNINGS 1

//1143. 最长公共子序列
class Solution {
public:
    int longestCommonSubsequence(string s1, string s2) {
        // 1.创建 dp 表
        // 2.初始化
        // 3.填表
        // 4.返回值

        int m = s1.size(), n = s2.size();
        s1 = ' ' + s1, s2 = ' ' + s2;// 处理下标映射关系
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));// 建表 + 初始化
        for (int i = 1; i <= m; ++i) // 从上往下每⼀⾏
            for (int j = 1; j <= n; ++j)// 每⼀⾏从左往右
                //if(s1[i - 1] == s2[j - 1])
                if (s1[i] == s2[j])
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                else
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        // 返回结果
        return dp[m][n];

    }
};